<?php

namespace App\Http\Service\Weibo;

use App\Models\Weibopost;
use App\Models\Weibolog;
use App\Models\Doubanlike;
use App\Models\Doubancollect;
use App\Http\Service\UserService;
use App\Http\Service\Douban\DoubanpostimageService;
use Illuminate\Support\Facades\DB;
use App\Http\Service\BaseService;

class WeibopostService  extends BaseService
{
    private $model;

    public function __construct(Weibopost $model,UserService $userService,Weibolog $weibolog,DoubanpostimageService $DoubanpostimageService,Doubanlike $doubanlike)
    {
        parent::__construct();
        $this->model = $model;
        $this->weibolog = $weibolog;
        $this->userService = $userService;
        $this->DoubanpostimageService = $DoubanpostimageService;
        $this->doubanlike = $doubanlike;
    }
   
    /**
     *
     * @param array $params
     * @return array
     */
    public function addData($request,$data=[])
    {
        $unique_id=$data['unique_id'];
        //获取所有json数据
        $jsonData = $request->json()->all();
        $data['json_data']=json_encode($jsonData, JSON_UNESCAPED_UNICODE);

        //dd($res);
        $post_data=[
            'json_data'=>$data['json_data'],
            'unique_id'=>$data['unique_id'],
            'article_id'=>$data['article_id'],
            'avatar'=>$data['avatar'],
            'user_id'=>$data['user_id'],
            'nickname'=>$data['nickname'],
            'content_books'=>$data['content'],
            'images'=>$data['images'],
            'has_retweet'=>$data['has_retweet'],
            'product_num'=>$data['product_num'],
            'retweeted_content'=>$data['retweeted_content'],
            'publish_time'=>$data['publish_time'],
            'is_weibo_chaohua'=>$data['is_weibo_chaohua'],
            'article_detail_url'=>$data['article_detail_url'],
            'like_count'=>'0',
            'collect_count'=>'0',
            'comment_count'=>'0',
            'view_count'=>'0',

            'status'=>'1',

        ];
        //dd($post_data);
        $post_info=$this->model->where(['unique_id'=>$data['unique_id']])->first();
        
        if($post_info){
            
            $post_data['create_time'] =date("Y-m-d H:i:s");
            //dd($post_data);
            $this->model->where(['unique_id'=>$data['unique_id']])->update($post_data);
            $post_id=$post_info['id'];

        }else{
            $post_data['create_time'] =date("Y-m-d H:i:s");
            $post_data['update_time'] =date("Y-m-d H:i:s");
            $post_id=$this->model->insertGetId($post_data);

        }
        //dd($post_id);
        return($post_data);


    }
    //微博线报昵称标签
    public function getwbLableList($request,$data=[])
    {
        $is_weibo_chaohua=$data['is_weibo_chaohua'];
        $sql="SELECT `nickname`,`user_id` FROM `module_weibo_post`   where is_weibo_chaohua='{$is_weibo_chaohua}'   GROUP BY `nickname`";
        $list = DB::select($sql);
        $list = array_map('get_object_vars', $list);
        
        return $list;
    }
    //列表
    public function getList($request,$data=[])
    {
       $page = intval($request->input('page')) ?$request->input('page') :$this->cpage;
        $limit = intval($request->input('limit')) ?$request->input('limit') :$this->climit;

        $offset = ($page-1)*$limit;

        $where = [
           ['status','=','1'],
          // ['id','=','5362'],
        ];
        //是否是微博超话
        if(isset($data['is_weibo_chaohua'])&&!empty($data['is_weibo_chaohua'])){
            $where[] = ['is_weibo_chaohua','=','1'];
        }else{
            $where[] = ['is_weibo_chaohua','!=','1'];
        }
        //dd($where);

        $where[] = [function($query) use ($data){
            $search_content=$data['search_content'];
            $user_id=$data['user_id'];
            if(!empty($search_content) && !empty($user_id)){
                $query->where('user_id',$user_id)
                ->whereRaw("concat(`content_books`,`retweeted_content`) like '%".$search_content."%'");

            }else if(!empty($search_content) && empty($user_id)){
                $query->whereRaw("concat(`content_books`,`retweeted_content`) like '%".$search_content."%'");
            }else if (empty($search_content) && empty(!$user_id)){
                $query->where('user_id',$user_id);
            }
            
            
        }];

            
        

        $count = $this->model->where($where)->count();
        $fields = ['id','unique_id','avatar','nickname','content_books','images','has_retweet','product_num','update_time','retweeted_content'];
        //$this->getLastSql();
        $list = $this->model->select($fields)
        ->where($where)
        ->offset($offset)->limit($limit)
        ->orderBy('update_time', 'desc')
        ->orderBy('publish_time', 'desc')
        ->orderBy('id', 'desc')
        ->get();

        foreach($list as &$v){
            $v['save_time'] =uc_time_ago($v['update_time']);
            if($v['has_retweet']){
                $retweeted_content=json_decode($v['retweeted_content'],true);
                $v['retweeted_content']=$retweeted_content;

            }
            $v['content_books'] =json_decode($v['content_books'],true); 
            
            
            $v['images'] =json_decode($v['images'],true);
        }
        $result = [
            'page'=> $page,
            'limit'=> $limit,
            'total_page'=> ceil($count/$limit),
            'count'=>$count,
            'data'=> $list
        ];
        return $result; 
    }
    //详情
    public function dataDetail($request,$data=[])
    {
        $unique_id=$data['unique_id'];
        $people_id=$data['people_id'];
        $where = [
           ['unique_id','=',$unique_id],
        ];
        $list = $this->model->where($where)->first();
        $likeList=Doubanlike::getList();
        $collectList=Doubancollect::getList();
        //dd($collectList);
        $key="{$list['unique_id']}_{$people_id}";
        $list['is_like'] =2;
        if(isset($likeList[$key])){
            $list['is_like'] =1;
        }
        $list['is_collect'] =2;
        if(isset($collectList[$key])){
            $list['is_collect'] =1;
        }
        $list['content_books'] =json_decode($list['content_books'],true);
        $list['images'] =json_decode($list['images'],true);
        
        return $list;
    }
   
}
